﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;

namespace G.Reader.View
{
    public partial class SettingsPage : GBasePage
    {
        public SettingsPage()
        {
            InitializeComponent();

        }
        bool isloaded = false;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            List<int> datasource = new List<int>();
            for (int a = 20; a < 41; a++)
            {
                datasource.Add(a);
            }
            listpicker_fontsize.ItemsSource = datasource;
            listpicker_fontsize.SelectedIndex = (int)GloabalData.Instance.Settings.FontSize - 20;
            isloaded = true;
        }

        private void listpicker_fontsize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (isloaded)
                GloabalData.Instance.Settings.FontSize = listpicker_fontsize.SelectedIndex + 20;
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        }

        private void btnAddBackground_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Phone.Tasks.PhotoChooserTask pt = new Microsoft.Phone.Tasks.PhotoChooserTask();
            pt.ShowCamera = false;
            pt.Completed += new EventHandler<Microsoft.Phone.Tasks.PhotoResult>(pt_Completed);
            pt.Show();
        }

        void pt_Completed(object sender, Microsoft.Phone.Tasks.PhotoResult e)
        {
            if (e.Error == null)
            {
                if (e.TaskResult == Microsoft.Phone.Tasks.TaskResult.OK)
                {
                    GloabalData.Instance.Settings.SetBackgroundImage(e.ChosenPhoto);
                }
            }
            else if (e.Error is InvalidOperationException)
            {
                this.ShowState("请断开手机和电脑的链接");
            }
        }

        private void btnRemoveBackground_Click(object sender, RoutedEventArgs e)
        {
            GloabalData.Instance.Settings.SetBackgroundImage(null);
        }


    }
}